package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.PrinterCertification;
import com.ruoyi.nongjt.service.PrinterCertificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 打印机认证情况管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/nongjt/printerCertification")
public class PrinterCertificationController extends BaseController
{
    @Autowired
    private PrinterCertificationService printerCertificationService;

    /**
     * 查询打印机认证情况管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrinterCertification printerCertification)
    {
        startPage();
        List<PrinterCertification> list = printerCertificationService.selectPrinterCertificationList(printerCertification);
        return getDataTable(list);
    }

    /**
     * 导出打印机认证情况管理列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:export')")
    @Log(title = "打印机认证情况管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PrinterCertification printerCertification)
    {
        List<PrinterCertification> list = printerCertificationService.selectPrinterCertificationList(printerCertification);
        ExcelUtil<PrinterCertification> util = new ExcelUtil<PrinterCertification>(PrinterCertification.class);
        util.exportExcel(response, list, "打印机认证情况管理数据");
    }

    /**
     * 获取打印机认证情况管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(printerCertificationService.selectPrinterCertificationById(id));
    }

    /**
     * 新增打印机认证情况管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:add')")
    @Log(title = "打印机认证情况管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrinterCertification printerCertification)
    {
        if ("1".equals(printerCertificationService.checkSerialNumberCertified(printerCertification.getSerialNumber())))
        {
            return error("新增打印机认证情况'" + printerCertification.getSerialNumber() + "'失败，该序列号已认证");
        }
        printerCertification.setCreateBy(getUsername());
        return toAjax(printerCertificationService.insertPrinterCertification(printerCertification));
    }

    /**
     * 修改打印机认证情况管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:edit')")
    @Log(title = "打印机认证情况管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrinterCertification printerCertification)
    {
        printerCertification.setUpdateBy(getUsername());
        return toAjax(printerCertificationService.updatePrinterCertification(printerCertification));
    }

    /**
     * 删除打印机认证情况管理
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:remove')")
    @Log(title = "打印机认证情况管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(printerCertificationService.deletePrinterCertificationByIds(ids));
    }

    /**
     * 获取打印机认证情况管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:query')")
    @GetMapping(value = "/checkSerialNumberCertified")
    public AjaxResult checkSerialNumberCertified(PrinterCertification printerCertification)
    {
        return success(printerCertificationService.checkSerialNumberCertified(printerCertification.getSerialNumber()));
    }

    /**
     * 导入打印机认证情况数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:import')")
    @Log(title = "打印机认证情况管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PrinterCertification> util = new ExcelUtil<PrinterCertification>(PrinterCertification.class);
        List<PrinterCertification> printerCertificationList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = printerCertificationService.importPrinterCertification(printerCertificationList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printerCertification:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<PrinterCertification> util = new ExcelUtil<PrinterCertification>(PrinterCertification.class);
        util.importTemplateExcel(response, "打印机认证情况数据");
    }
}
